package it.datagraf.formazione.hellostruts.service;

import it.datagraf.formazione.hellostruts.dao.CommentoDao;
import it.datagraf.formazione.hellostruts.dao.CompetenzaDao;
import it.datagraf.formazione.hellostruts.dao.CurriculumVitaeDaoHbm;
import it.datagraf.formazione.hellostruts.entity.Commento;
import it.datagraf.formazione.hellostruts.entity.Competenza;
import it.datagraf.formazione.hellostruts.entity.CurriculumVitae;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public class CVserviceImpl implements CVServiceInteface {
	
	
	CurriculumVitaeDaoHbm cvDao;
	CompetenzaDao competenzaDao;
	CommentoDao commentoDao;
	
	public void setCvDao(CurriculumVitaeDaoHbm cvDao) {
		this.cvDao = cvDao;
	}
	
	public CurriculumVitaeDaoHbm getCvDao(){
		return cvDao;
	}
	
	
	
	public CompetenzaDao getCompetenzaDao() {
		return competenzaDao;
	}

	public void setCompetenzaDao(CompetenzaDao competenzaDao) {
		this.competenzaDao = competenzaDao;
	}
	
	
	
	
	
	
	
	// static Properties jdbcProps= new Properties();
	// static {
	// try {
	// jdbcProps.load(Class.class.getResourceAsStream("jdbc-connection.properties"));
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

//	private String url;
//	public String getUrl() {
//		return url;
//	}
//
//	public void setUrl(String url) {
//		this.url = url; 
//	}
//
//	public String getLogin() {
//		return login;
//	}
//
//	public void setLogin(String login) {
//		this.login = login;
//	}
//
//	public String getPassword() {
//		return password;
//	}
//
//	public void setPassword(String password) {
//		this.password = password;
//	}
//
//	private String login;
//	private String password;
	
	


	public CommentoDao getCommentoDao() {
		return commentoDao;
	}

	public void setCommentoDao(CommentoDao commentoDao) {
		this.commentoDao = commentoDao;
	}

	Connection getConnection() throws SQLException {

		try {

			Class.forName("org.postgresql.Driver");

		} catch (ClassNotFoundException e) {
			System.out.println("Where is your PostgreSQL JDBC Driver? "
					+ "Include in your library path!");
			e.printStackTrace();
		}

		return DriverManager.getConnection(
				"jdbc:postgresql://localhost:5432/cv", "postgres", "admin");
	}


//	public List<CurriculumVitae> listCvs() {
//		List<CurriculumVitae> results = new ArrayList<CurriculumVitae>();
//		CurriculumVitaeDao cvDao = null;
//		try {
//			results = getCvDao().list();
//		} catch (SQLException e) {
//			throw new RuntimeException(e);
//		} finally {
//			if (cvDao != null)
//				cvDao.closeConnection();
//		}
//		return results;
//	}
	
	public List<CurriculumVitae> listCvs(){
		
		return cvDao.list();
	}

	public List<Commento> listCommenti(int id_cv) {
//		List<Commento> results = new ArrayList<Commento>();
//		CurriculumVitaeDao cvDao = null;
//		try {
//			results = getCvDaoO().listCommenti(id_cv);
//		} catch (SQLException e) {
//			throw new RuntimeException(e);
//		} finally {
//			if (cvDao != null)
//				cvDao.closeConnection();
//		}
//		return results;
		try {
			return getCommentoDao().listCommenti(id_cv);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void inserisciCommento(Commento com) {
//		CurriculumVitaeDaoI cvDao = null;
//		try {
//			cvDao = getCvDaoO();
//			cvDao.insertCommento(com);
//		} catch (SQLException e) {
//			throw new RuntimeException(e);
//		} finally {
//			if (cvDao != null)
//				cvDao.closeConnection();
//		}
		
		try {
			getCommentoDao().insertCommento(com);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void insertCV(CurriculumVitae cv) throws SQLException {
		getCvDao().insertCV(cv);
	}

	public void update(CurriculumVitae cv) throws SQLException {
		getCvDao().update(cv);
	}

	public void remove(long id) throws SQLException {
		getCvDao().remove(id);
	}

	public int findId(String owner) throws SQLException {

		return getCvDao().findId(owner);
	}

	public CurriculumVitae find(CurriculumVitae cv) throws SQLException {

		return getCvDao().find(cv);

	}

	public CurriculumVitae find(int id) {

		try {
			return getCvDao().find(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void inserisciCompetenza(Competenza comp) {
		getCompetenzaDao().saveOrUpdate(comp);
	}

	//lista delle competenze
	public List<Competenza> listComps() {
		
		try {
			return getCompetenzaDao().listComps();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public void removeComp(long id) {
		try {
			getCompetenzaDao().removeComp(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public List<CurriculumVitae> listCvs_ricerca(String Cognome, String Nome, String eta) {
//		
//			List<CurriculumVitae> lista_ricerca = new ArrayList<CurriculumVitae>();
//			CurriculumVitaeDao cv_ricerca = null;
//			try {
//				lista_ricerca = getCvDaoO().listCvs_ricerca(Cognome, Nome, eta);				
//			} catch (SQLException e) {
//				throw new RuntimeException(e);
//			} finally {
//				if (cv_ricerca != null)
//					cv_ricerca.closeConnection();
//			}
//			return lista_ricerca;
		return null;
		
	}
	
	@Override
	public Competenza getCompetenza(Long id){
		return competenzaDao.get(id);
	}

	@Override
	public List<Competenza> getRootNodes() {
		return competenzaDao.getRootNodes();
	}

	
}
